﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using lib.Twitter;
using lib;
using System.Net;

namespace one4all_UI
{
    public delegate void SListItemClicked(int ind, bool right);
    delegate void tRefreshItemsCallback();

    public class SListBox : ListBox
    {

        Font f_small = new Font("Microsoft Sans Serif", 8, FontStyle.Regular);
        Font f_small_b = new Font("Microsoft Sans Serif", 8, FontStyle.Bold);


        private Dictionary<String, Image> imageCache;
        //Delegate sem skilar selected index þegar klikkað er á item í ListBoxi. DELEGATES A.
        public SListItemClicked SListItemClicked = delegate { };
        //invoke the changed event
        public SListBox()
        {
            //init image cach
            imageCache = new Dictionary<String, Image> { };
            //Load Cache using a newThread
            ThreadStart ts = delegate
            {
                List<User> u = TwitterLinq.getTwitterUsers();
                foreach (User us in u)
                {
                    try
                    {
                        if (!imageCache.ContainsKey(us.screen_name))
                        {
                            Image i = Util.download(new Uri(us.profile_image_url));
                            if (i.Width > 50)
                            {
                                i = Util.Resize(i, 50);
                            }
                            imageCache.Add(us.screen_name, i);
                            this.tRefreshItems();
                        }
                    }
                    catch
                    {
                        //Do nothing
                    }

                }
                Debug.WriteLine("ImageCache is Done");
            };

            //Make sure it is fast
            Thread t1 = new Thread(ts);
            t1.Priority = ThreadPriority.Highest;
            t1.Start();

            // Set owner draw mode
            this.DrawMode = DrawMode.OwnerDrawFixed;
            // Delegate activated on click. DELEGATES B.
            this.MouseDown += (s, e) =>
            {
                MouseEventArgs foo = (MouseEventArgs)e;
                this.SListItemClicked(
                    this.IndexFromPoint(((MouseEventArgs)e).X, ((MouseEventArgs)e).Y),
                    (foo.Button == System.Windows.Forms.MouseButtons.Right));
            };
        }


        protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs e)
        {
            e.DrawBackground();
            e.DrawFocusRectangle();
            Rectangle bounds = e.Bounds;

            if (e.Index != -1)
            {
                StatusUser item;
                Font f = new Font(this.Font, FontStyle.Bold);
                Rectangle statusRect = bounds;
                statusRect.Height = this.ItemHeight;
                statusRect.Width = bounds.Width - (50 + 4);
                statusRect.Location = new Point(bounds.X + (50 + 2), bounds.Y + f.Height);
                try
                {
                    item = (StatusUser)Items[e.Index];
                    //Draw image
                    if (imageCache.ContainsKey(item.user.screen_name))
                    {
                        e.Graphics.DrawImage(imageCache[item.user.screen_name], bounds.Left + 2, bounds.Top + 2,48,48);
                    }
                    else
                    {
                        e.Graphics.DrawString("loading..", f_small, new SolidBrush(e.ForeColor), bounds.Left + 5, bounds.Top + 20);
                    }
                    //Draw title 

                    e.Graphics.DrawString(item.user.name + "   (" + item.user.screen_name + ")", f, new SolidBrush(e.ForeColor),
                        bounds.Left + 50, bounds.Top + 2);
                    //Draw text
                    bool r = (item.status.read == 1);
                    e.Graphics.DrawString(item.status.text, r ? f_small : f_small_b, new SolidBrush(e.ForeColor),
                           statusRect);
                }
                catch (Exception ex)
                {
                    e.Graphics.DrawString(Items[e.Index].ToString(), e.Font,
                        new SolidBrush(e.ForeColor), bounds.Left, bounds.Top);
                }
            }
            else
            {
                e.Graphics.DrawString("No statuses available", e.Font, new SolidBrush(e.ForeColor),
                    bounds.Left, bounds.Top);
            }

            base.OnDrawItem(e);
        }


        //Refresh list
        public void tRefreshItems()
        {
            if (this.InvokeRequired)
            {
                tRefreshItemsCallback d = new tRefreshItemsCallback(tRefreshItems);
                this.Invoke(d, new object[] {  });
            }
            else
            {
                this.Refresh();
            }
        }

    }

}
